﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	
	public class VoicesDisplayer extends MovieClip {
		static public const USER_VOICES = 1297;
		private var main: Main = null;
		private var _votes: Number = 15;
		
		public function get votes(): Number {
			return _votes;
		}
		
		public function set votes (value: Number): void {
			_votes = value;
			main.vKontakte.api("putVariable", { key: USER_VOICES, value: votes, user_id: main.viewerID },
				uploadOk, uploadFailed);
			labelVoices.text = ""+_votes;
		}	
		
		public function VoicesDisplayer() {
			labelVoices.text = "15";
			
			root.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		public function onAddedToStage(e: Event): void {
			main = Main(root);
			main.votes = this;
			addEventListener(MouseEvent.CLICK, onClick);
			main.vKontakte.api("getVariable", { key: USER_VOICES, value: votes, user_id: main.viewerID }, 
				downloadOk, downloadFailed);
		}
		
		private function downloadOk(e: Object) {
			votes = Number(e);
		}
		
		private function downloadFailed(e: Object): void {
			trace("Votes download failed: ", e);
		}
		
		private function uploadOk(e: Object) {
			trace("Voices uploaded");
		}
		
		private function uploadFailed(e: Object): void {
			trace("Voices uploading has been failed");
		}
		public function onClick(e: MouseEvent): void {
			if ( ! main.localWork) {
				main.vKontakte.callMethod("showPaymentBox");
			}
		}
	}
	
}
